package cc.ttiot.service.dataCollection.io.impl;

import cc.ttiot.service.dataCollection.io.IoCollection;
import com.pi4j.io.gpio.*;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;


/**
 * @Description:
 * @Author :
 * @Version :
 * @Date :
 */
public class ApproachingSwitchCollection implements IoCollection {
    private static volatile ApproachingSwitchCollection approachingSwitchCollection;

    //将接近开关引脚设置为数字输入引脚，启用其内部上拉电阻器(因为目前选用接近开关为NPN常开型)
    private final GpioPinDigitalInput approachingSwitchDigitalInput = GPIO.provisionDigitalInputPin(IoCollection.APPROACHING_SWITCH_PIN, PinPullResistance.PULL_UP);


    private ApproachingSwitchCollection() {
        super();
    }

    //双检索单例防止多次初始化
    public static ApproachingSwitchCollection getInstance() {
        if (approachingSwitchCollection == null) {
            synchronized (ApproachingSwitchCollection.class) {
                if (approachingSwitchCollection == null) {
                    approachingSwitchCollection = new ApproachingSwitchCollection();
                }
            }
        }
        return approachingSwitchCollection;
    }

    public GpioPinDigitalInput getApproachingSwitchDigitalInput() {
        return approachingSwitchDigitalInput;
    }

    @Override
    public PinState getState() {
        return approachingSwitchDigitalInput.getState();
    }

}
